﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using Telerik.Windows.Controls;
using Conqueror.WP.Model;
using Conqueror.WP.ViewModel.Login;
using Conqueror.WP.Utility;
using Microsoft.Phone.Tasks;

namespace Conqueror.WP
{
    public partial class LoginPage : PhoneApplicationPage
    {
        private LoginViewModel loginViewModel;

        public LoginPage()
        {
            InitializeComponent();

            loginViewModel = new LoginViewModel();
        }

        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            //Clear back stack. This page may never have a back event.
            while (NavigationService.CanGoBack)
                NavigationService.RemoveBackEntry();

            base.OnNavigatedTo(e);
            loginViewModel = new LoginViewModel();
            DataContext = loginViewModel;
            animHideLoginOverlay.Begin();
            UserLogin.LoginFailure += OnLoginFailure;
            UserLogin.LoginSuccess += OnLoginSuccess;

            if (!App.FirstLaunch)
            {
                while(NavigationService.BackStack.Count() != 0) //Clear back
                    NavigationService.RemoveBackEntry();
            }
            else
            {
                App.FirstLaunch &= false;
                if (!String.IsNullOrWhiteSpace(User.Username))
                {
                    BeginLogin();
                }
            }
        }

        protected override void OnNavigatedFrom(NavigationEventArgs e)
        {
            base.OnNavigatedFrom(e);
            UserLogin.LoginFailure -= OnLoginFailure;
            UserLogin.LoginSuccess -= OnLoginSuccess;
        }

        private void OnLoginSuccess(object sender, EventArgs e)
        {
            Dispatcher.BeginInvoke(() =>
            {
                NavigationService.Navigate(new Uri("/MainPage.xaml", UriKind.Relative));
            });
        }

        private void OnLoginFailure(object sender, EventArgs e)
        {
            Dispatcher.BeginInvoke(() => {
                animHideLoginOverlay.Begin();
                loginViewModel.Enabled = true;

                showFailLogin = true;
                animDisplayLoginFail.Begin();
            });
        }

        private void btnBeginLogin(object sender, RoutedEventArgs e)
        {
            if (showFailLogin)
            {
                animHideLoginFail.Begin();
                showFailLogin = false;
            }
            BeginLogin();
        }

        private void BeginLogin()
        {
            loginViewModel.LoginMessage = UserLogin.GetValidatingAccountString();
            loginViewModel.Enabled = false;
            Dispatcher.BeginInvoke(() =>
            {
                animDisplayLoginOverlay.Begin();
            });

            UserLogin.AttemptUserLogin(loginViewModel);
        }

        private bool showFailLogin = false;

        private void HideMessageDisplay(object sender, TextChangedEventArgs e)
        {
            if (showFailLogin)
            {
                animHideLoginFail.Begin();
                showFailLogin = false;
            }
        }

        private void RegisterNewAccount(object sender, System.Windows.Input.GestureEventArgs e)
        {
            WebBrowserTask task = new WebBrowserTask();
            task.Uri = new Uri("http://www.conquerclub.com/?ref=427135", UriKind.Absolute);
            task.Show();
        }
    }
}